package cn.iocoder.yudao.module.biz.service.expand.targets;

import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.ExpandTargetViewDO;
import cn.iocoder.yudao.module.biz.dal.mysql.expand.targets.ExpandTargetViewMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.ExpandTargetDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.expand.targets.ExpandTargetMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 拓展目标 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ExpandTargetServiceImpl implements ExpandTargetService {

    @Resource
    private ExpandTargetMapper expandTargetMapper;

    @Resource
    private ExpandTargetViewMapper expandTargetViewMapper;

    @Override
    public Long createExpandTarget(ExpandTargetSaveReqVO createReqVO) {
        // 插入
        ExpandTargetDO expandTarget = BeanUtils.toBean(createReqVO, ExpandTargetDO.class);
        expandTargetMapper.insert(expandTarget);

        // 返回
        return expandTarget.getId();
    }

    @Override
    public void updateExpandTarget(ExpandTargetSaveReqVO updateReqVO) {
        // 校验存在
        validateExpandTargetExists(updateReqVO.getId());
        // 更新
        ExpandTargetDO updateObj = BeanUtils.toBean(updateReqVO, ExpandTargetDO.class);
        expandTargetMapper.updateById(updateObj);
    }

    @Override
    public void deleteExpandTarget(Long id) {
        // 校验存在
        validateExpandTargetExists(id);
        // 删除
        expandTargetMapper.deleteById(id);
    }

    @Override
        public void deleteExpandTargetListByIds(List<Long> ids) {
        // 删除
        expandTargetMapper.deleteByIds(ids);
        }


    private void validateExpandTargetExists(Long id) {
        if (expandTargetMapper.selectById(id) == null) {
            throw exception(BIZ_EXPAND_TARGET_NOT_EXISTS);
        }
    }

    @Override
    public ExpandTargetDO getExpandTarget(Long id) {
        return expandTargetMapper.selectById(id);
    }

    @Override
    public PageResult<ExpandTargetDO> getExpandTargetPage(ExpandTargetPageReqVO pageReqVO) {
        return expandTargetMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<ExpandTargetViewDO> getAllExpandTargetPage(ExpandTargetPageReqVO pageReqVO) {
        return expandTargetViewMapper.selectAllExpandTargetPage(pageReqVO);
    }

    @Override
    public List<ExpandTargetViewDO> getExpandTargetList(ExpandTargetPageReqVO pageReqVO) {
        return expandTargetViewMapper.selectAllExpandTarget(pageReqVO);
    }
}